package com.cskaoyan.controller.wxorder;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MemoryBean;
import com.cskaoyan.bean.bo.wxorder.WxFeedbackBo;
import com.cskaoyan.bean.po.adminpo.MarketAdmin;
import com.cskaoyan.bean.po.ohter.MarketUser;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.wxorder.WxFeedbackService;
import com.cskaoyan.util.common.Constant;
import com.cskaoyan.util.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-19 20:35
 */
@RestController
@RequestMapping("wx/feedback")
public class WxFeedbackController {
    @Autowired
    MemoryBean memoryBean;
    @Autowired
    WxFeedbackService feedbackService;
    @Autowired
    MarketUserMapper marketUserMapper;

    @PostMapping("submit")
    public BaseRespVo wxFeedback(@RequestHeader(Constant.WX_TOKEN_HEADER) String token,
                                 @Validated @RequestBody WxFeedbackBo wxFeedback, BindingResult result) throws MarketValidationException {
        ValidationUtils.valid(result);

        Integer id = memoryBean.tokenAndUserId.get(token);
        if(id==null){
            throw new MarketValidationException("请重新登陆！");
        }
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(id);

        String username= marketUser.getUsername();
        wxFeedback.setUserId(id);
        wxFeedback.setUsername(username);
        feedbackService.updateFeedBack(wxFeedback);
        return BaseRespVo.ok();
    }
}
